package com.example.androidautoproject.utils

import android.annotation.SuppressLint
import android.os.Handler
import android.os.Looper
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import com.blankj.utilcode.util.ScreenUtils
import com.example.androidautoproject.cwdb.CwdbProject
import com.example.androidautoproject.cwdb.KadayxProject
import com.example.androidautoproject.cwdb.TzwjxProject
import com.example.androidautoproject.databinding.OverlayBinding
import com.example.androidautoproject.hlxn.FxyProject
import com.example.androidautoproject.lddj.LddjProject
import com.example.androidautoproject.lddj.ZjskProject
import com.example.androidautoproject.service.FengyiFreeProject
import com.example.androidautoproject.mmly.MmlyProject
import com.example.androidautoproject.service.SlideAuto
import com.example.androidautoproject.service.TouTiaoProject
import com.example.androidautoproject.service.UcProject
import com.example.androidautoproject.service.WeChatLoginAuto
import com.example.androidautoproject.kxh.SyspProject
import com.example.androidautoproject.hlxn.HlxnProject
import com.example.androidautoproject.kxh.KxhProject
import com.example.androidautoproject.hlxn.LkjxProject
import com.example.androidautoproject.hlxn.LxsProject
import com.example.androidautoproject.hlxn.TxddsProject
import com.example.androidautoproject.kxh.XzzProject1
import com.example.androidautoproject.kxh.ZcttProject
import com.example.androidautoproject.mmly.MfmwProject1
import com.example.androidautoproject.mmly.MhlyProject
import com.example.androidautoproject.qtmm2.HccsfProject
import com.example.androidautoproject.qtmm2.qtmmProject
import com.example.androidautoproject.sghl.SghlProject
import com.example.androidautoproject.ylpp.AysgProject
import com.example.androidautoproject.ylpp.JyhhProject
import com.example.androidautoproject.ylpp.SsytProject
import com.example.androidautoproject.ylpp.SsyyProject
import com.example.androidautoproject.ylpp.YlppProject
import com.example.androidautoproject.ylpp.ggjyjProject
import com.ven.assists.service.AssistsService
import com.ven.assists.service.AssistsServiceListener
import com.ven.assists.stepper.StepManager
import com.ven.assists.window.AssistsWindowManager
import com.ven.assists.window.AssistsWindowWrapper

object Overlay: AssistsServiceListener {
    private val TAG = "fengyiFreeProject"
    private val handler = Handler(Looper.getMainLooper())
    private var logObserver: ((String) -> Unit)? = null
    @SuppressLint("StaticFieldLeak")
    var viewBinding: OverlayBinding? = null
        private set
        get() {
            if(field == null){
                field = OverlayBinding.inflate(LayoutInflater.from(AssistsService.instance)).apply{
                    btnCash.setOnClickListener{
                        StepManager.execute(FengyiFreeProject::class.java, stepTag = 1, begin = true )
                    }

                    btnAd.setOnClickListener{
                        StepManager.execute(WeChatLoginAuto::class.java, stepTag = 1, begin = true )
                    }
                    btnLog.setOnClickListener{
                        OverlayLog.show()
                    }
                    btnUc.setOnClickListener{
                        StepManager.execute(UcProject::class.java, stepTag = 1, begin = true )
                    }

                    btnTt.setOnClickListener{
                        StepManager.execute(TouTiaoProject::class.java, stepTag = 1, begin = true )
                    }

                    btnMmly.setOnClickListener{
                        StepManager.execute(MmlyProject::class.java, stepTag = 1, begin = true )
                    }
                    btnSysp.setOnClickListener{
                        StepManager.execute(SyspProject::class.java, stepTag = 1, begin = true )
                    }
                    btnHlxn.setOnClickListener{
                        StepManager.execute(HlxnProject::class.java, stepTag = 1, begin = true )
                    }
                    btnSghl.setOnClickListener{
                        StepManager.execute(SghlProject::class.java, stepTag = 1, begin = true )
                    }
                    btnYlpp.setOnClickListener{
                        StepManager.execute(YlppProject::class.java, stepTag = 1, begin = true )
                    }
                    btnKxh.setOnClickListener{
                        StepManager.execute(KxhProject::class.java, stepTag = 1, begin = true )
                    }
                    btnLkjx.setOnClickListener{
                        StepManager.execute(LkjxProject::class.java, stepTag = 1, begin = true )
                    }
                    btnLddj.setOnClickListener{
                        StepManager.execute(LddjProject::class.java, stepTag = 1, begin = true )
                    }
                    btnZjsk.setOnClickListener{
                        StepManager.execute(ZjskProject::class.java, stepTag = 1, begin = true )
                    }
                    btnZctt.setOnClickListener{
                        StepManager.execute(ZcttProject::class.java, stepTag = 1, begin = true )
                    }
                    btnJyhh.setOnClickListener{
                        StepManager.execute(JyhhProject::class.java, stepTag = 1, begin = true )
                    }
                    btnGgjyj.setOnClickListener{
                        StepManager.execute(ggjyjProject::class.java, stepTag = 1, begin = true )
                    }
                    btnFxy.setOnClickListener{
                        StepManager.execute(FxyProject::class.java, stepTag = 1, begin = true )
                    }
                    btnTxdds.setOnClickListener{
                        StepManager.execute(TxddsProject::class.java, stepTag = 1, begin = true )
                    }
                    btnLxs.setOnClickListener{
                        StepManager.execute(LxsProject::class.java, stepTag = 1, begin = true )
                    }
                    btnAysg.setOnClickListener{
                        StepManager.execute(AysgProject::class.java, stepTag = 1, begin = true )
                    }
                    btnSsyy.setOnClickListener{
                        StepManager.execute(SsyyProject::class.java, stepTag = 1, begin = true )
                    }
                    btnSsyt.setOnClickListener{
                        StepManager.execute(SsytProject::class.java, stepTag = 1, begin = true )
                    }
                    btnMhly.setOnClickListener{
                        StepManager.execute(MhlyProject::class.java, stepTag = 1, begin = true )
                    }
                    btnQtmm.setOnClickListener{
                        StepManager.execute(qtmmProject::class.java, stepTag = 1, begin = true )
                    }
                    btnHccsf.setOnClickListener{
                        StepManager.execute(HccsfProject::class.java, stepTag = 1, begin = true )
                    }
                    btnMfmw.setOnClickListener{
                        StepManager.execute(MfmwProject1::class.java, stepTag = 1, begin = true )
                    }
                    btnCwdb.setOnClickListener{
                        StepManager.execute(CwdbProject::class.java, stepTag = 1, begin = true )
                    }
                    btnKayx.setOnClickListener{
                        StepManager.execute(KadayxProject::class.java, stepTag = 1, begin = true )
                    }
                    btnTzwjx.setOnClickListener{
                        StepManager.execute(TzwjxProject::class.java, stepTag = 1, begin = true )
                    }
                    btnXzz.setOnClickListener{
                        StepManager.execute(XzzProject1::class.java, stepTag = 1, begin = true )
                    }
                    btnStartSlide.setOnClickListener{
                        if(!AdParam.isStart){
                            StepManager.execute(SlideAuto::class.java, stepTag = 1, begin = true)
                        }
                    }
                    btnEndSlide.setOnClickListener{
                        AdParam.isStart = false
                        StepManager.isStop = true
                    }

                    // 日志观察者
                    logObserver = { message: String ->
                        handler.post {
                            // 更新TextView
                            // tvLog.append("$message\n")

                            // 自动滚动到底部
                            // scrollView.post {
                                // scrollView.fullScroll(ScrollView.FOCUS_DOWN)
                            // }
                        }
                    }

                    // 注册观察者
                    logObserver?.let { observer ->
                        AdParam.registerObserver(observer)
                    }

                }
            }
            return field
        }

    var onClose:((parent: View) -> Unit) ?= null
    var showed = false
        private set
        get() {
            assistWindowWrapper?.let {
                return AssistsWindowManager.isVisible(it.getView())
            } ?: return false
        }

    var assistWindowWrapper: AssistsWindowWrapper? = null
        private set
        get() {
            viewBinding?.let{
                if(field == null){
                    field = AssistsWindowWrapper(it.root, wmLayoutParams = AssistsWindowManager.createLayoutParams().apply {
                        width = (ScreenUtils.getScreenWidth()* 0.8).toInt()
                        height =  (ScreenUtils.getScreenHeight() * 0.5).toInt()
                    }, onClose = this.onClose).apply {
                        minWidth = (ScreenUtils.getScreenWidth() * 0.6).toInt()
                        minHeight = (ScreenUtils.getScreenHeight() * 0.4).toInt()
                        initialCenter = true
                        viewBinding.tvTitle.text = "安卓自动化"
                        Log.d(TAG, "onClose安卓自动化")
                    }
                }
            }
            return field
        }
    fun show(){
        if(!AssistsService.listeners.contains(this)){
            AssistsService.listeners.add(this)
        }
        AssistsWindowManager.add(assistWindowWrapper)
    }

    fun hide(){
        AssistsWindowManager.removeView(assistWindowWrapper?.getView())
    }

    override fun onUnbind() {
        super.onUnbind()
        viewBinding = null
        assistWindowWrapper = null
        // 取消注册观察者，避免内存泄漏
        logObserver?.let { observer ->
            AdParam.unregisterObserver(observer)
        }
    }
}
